package com.yyx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyx.entity.Category;
import com.yyx.entity.Dish;
import com.yyx.entity.DishFlavor;
import com.yyx.entity.dto.DishDto;
import com.yyx.mapper.DishMapper;
import com.yyx.service.CategoryService;
import com.yyx.service.DishFlavorService;
import com.yyx.service.DishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜品管理 服务实现类
 * </p>
 *
 * @author yeyinxuan
 * @since 2023-07-31
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    DishFlavorService dishFlavorService;
    @Autowired
    DishService dishService;
    @Autowired
    CategoryService categoryService;


    @Override
    public Page<DishDto> selectpage(int page, int pageSize, String name) {

        Page<Dish> page1 = new Page<>(page, pageSize);
        Page<DishDto> dishDtoPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(name),Dish::getName,name);
        lambdaQueryWrapper.orderByAsc(Dish::getCategoryId,Dish::getId);

        this.page(page1,lambdaQueryWrapper);
        BeanUtils.copyProperties(page1,dishDtoPage,"records");

        //将分页查询的结果 做stream流
        List<Dish> records = page1.getRecords();
//        List<Dish> list = dishService.list(lambdaQueryWrapper);
        List<DishDto> dishDtos = records.stream().map((item -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                dishDto.setCategoryName(category.getName());
            }
            return dishDto;
        })).collect(Collectors.toList());
        dishDtoPage.setRecords(dishDtos);
        return dishDtoPage;
    }

    @Transactional
    @Override
    public Boolean saveWithflavor(DishDto dishDto) {
//        基本信息
        this.save(dishDto);
//        菜品id
        Long dishId = dishDto.getId();

        List<DishFlavor> dishFlavors = dishDto.getFlavors();
//        dishFlavors.forEach(f->{
//            f.setDishId(dishId);
//        });

        for (DishFlavor f : dishFlavors) {
            f.setDishId(dishId);
        }
//        保存口味
        dishFlavorService.saveBatch(dishFlavors);
        return true;
    }

    @Override
    public DishDto getDishDtoByid(Long id) {

        DishDto dishDto = new DishDto();
        Dish dish = this.getById(id);
        BeanUtils.copyProperties(dish,dishDto);

        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());

//        返回对象是list集合，需要用LambdaQueryWrapper查询并用list集合接受
        List<DishFlavor> flavor = dishFlavorService.list(queryWrapper);

//        dishFlavorService.lambdaUpdate().set(DishFlavor::getDishId,id).eq(DishFlavor::getUpdateTime,sd).update();

        dishDto.setFlavors(flavor);
        return dishDto;
    }
}
